<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/16
 * Time: 14:09
 */

namespace App\LogicService;

use App\LogicService\Contracts\UserGroupLogicContract;
use App\Models\UserGroup;
use App\Models\UserGroupMember;


class UserGroupLogic extends BaseLogic implements UserGroupLogicContract
{


    //创建群组
    /**
     * @param $data
     * @return mixed
     */
    public function store($data)
    {
        $data['type'] = 1;
        $id = UserGroup::create($data);
        return $id;

    }

    //解散群组
    public function delete($group_id)
    {
        $result = false;
        $userGroup = UserGroup::find($group_id);
        if ($userGroup) {
            $result = $userGroup->delete();
        }
        return $result;
    }

    //判断群组是否存在
    public function isGroup($account, $group_id)
    {
        $is_friend = 0;
        $userGroup = UserGroup::where('id', $group_id)
            ->where('account', $account)
            ->where('status', 1)
            ->first();

        if ($userGroup) {
            $is_friend = 1;
        }
        return $is_friend;
    }

    //获取群组列表
    public function getList($account)
    {
        $userGroups = UserGroup::where('st_user_group_member.user_account', $account)
            ->leftJoin('st_user_group_member', 'st_user_group.id', '=', 'st_user_group_member.group_id')
            ->select('st_user_group.*')
            ->groupBy('st_user_group.id')
            ->distinct()
            ->get();

        return $userGroups;
    }

    //获取群组列表
    public function update($group_id, $data)
    {
        $userGroups = UserGroup::where('id', $group_id)
            ->update($data);
        return $userGroups;
    }


    //获取群组信息
    public function show($group_id)
    {
        $userGroups = UserGroup::find($group_id);
        return $userGroups;
    }


}